package com.kun.portal.webframe.controller;

import com.kun.portal.webframe.po.BuyGoodsPo;
import com.kun.portal.webframe.po.BuyOrderPo;
import com.kun.portal.webframe.po.PurchasePlanPo;
import com.kun.portal.webframe.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018-07-10.
 */
@Controller
@RequestMapping({"/api/purchaseMgr"})
public class PurchaseMgrController {
    @Autowired
    private PurchaseService purchaseService;
    @InitBinder
    public void intDate(WebDataBinder dataBinder){
        dataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }
    @RequestMapping(
            value = {"/findPurchasePlanPo"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public List<PurchasePlanPo> findAllPurchasePlanPo(){
        return  purchaseService.findAllPurchasePlanPo();
    }
    @RequestMapping(
            value = {"/savePurchasePlanPo"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public void savePurchasePlanPo(@RequestBody PurchasePlanPo purchasePlanPo){
        purchaseService.savePurchasePlanPo(purchasePlanPo);

    }
    @RequestMapping(
            value = {"/delectPurchasePlanPo"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public void deletePurchasePlanPo(@RequestBody PurchasePlanPo purchasePlanPo){
        purchaseService.deletePurchasePlanPo(purchasePlanPo);

    }
    @RequestMapping(
            value = {"/checkPurchasePlanPo"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public List<PurchasePlanPo> purchasePlanPo(@RequestParam(required = false) String goodsCode ,
                                               @RequestParam(required = false) Date needDate,
                                               @RequestParam(required = false) String userDept){
        return  purchaseService.checkPurchasePlanPo(goodsCode,needDate,userDept);
    }
    @RequestMapping(
            value = {"/getPurchasePlanPoByState"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public List<PurchasePlanPo> getPurchasePlanPoByState(){
        return  purchaseService.getPurchasePlanPoByState();
    }
    @RequestMapping(
            value = {"/findBuyOrderPo"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public List<BuyOrderPo> findAllBuyOrderPo(){
        return purchaseService.findAllBuyOrderPo();

    }
    @RequestMapping(
            value = {"/checkBuyOrderPo"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public List<BuyOrderPo> findBuyOrderPo(@RequestParam(required = false) String buyNum,
                                               @RequestParam(required = false) Date purchaseDate,
                                               @RequestParam(required = false) String auditStatus){
        return  purchaseService.findBuyOrderPo(buyNum,purchaseDate,auditStatus);
    }

    @RequestMapping(
            value = {"/delectBuyOrderPo"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public void delectBuyOrderPo(@RequestBody BuyOrderPo buyOrderPo){
        purchaseService.delectBuyOrderPo(buyOrderPo);

    }
    @RequestMapping(
            value = {"/saveBuyOrderPo"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public void saveBuyOrderPo(@RequestBody BuyOrderPo buyOrderPo){
        List<BuyGoodsPo> buyGoodsPos1= purchaseService.saveBuyGoodsPos(buyOrderPo.getBuyGoodsPos());
        buyOrderPo.setBuyGoodsPos(buyGoodsPos1);
        purchaseService.saveBuyOrderPo(buyOrderPo);

    }

}
